<?php

namespace app\admin\controller;

use app\common\model\Classes;
use app\common\model\ClassesTingke;
use app\common\model\ClassesXueyuan;
use app\common\model\KaoqinRemark;
use app\common\model\KaoqinXueyuan;
use app\common\model\XueyuanJiaofei;
use app\common\model\Kaoqin as KaoqinModel;
use app\common\model\Xueyuan;

class Kaoqin extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array();

        if (input('manager_name')) {
            $where['manager_name'] = input('manager_name');
        }
        if (input('status')) {
            $where['status'] = input('status');
        }

        if (input('get.time/a')[0]) {
            $where['time'] = array(
                array('>=', input('get.time/a')[0]),
                array('<=', input('get.time/a')[1]),
            );
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit', 20);
        //获取分页列表
        $list = KaoqinModel::getPage($where, $page, $size);
        //总页码
        $total = KaoqinModel::getCount($where);
        //总出勤人数
        $chuqin_num = KaoqinModel::getSum($where,'num');

        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        //学员信息
        $cids = array_column($list, 'cid');
        $sids = ClassesXueyuan::getColumn(array('cid' => array('in', $cids)), 'sid');
        $xueyuan = Xueyuan::getColumn(array('id' => array('in', $sids)), 'id,name');
        $banji = Classes::getColumn(array('id' => array('in', $cids)), '*');
        $week = ['', '一', '二', '三', '四', '五', '六', '日'];

        foreach ($list as $k => $v) {
            //班级信息
            if ($v['cid'] && isset($banji[$v['cid']])) {
                $list[$k]['class_name'] = '周' . $week[$banji[$v['cid']]['week']] . $banji[$v['cid']]['address'] . $banji[$v['cid']]['time'] . '<br>' . $banji[$v['cid']]['name'];
            } else {
                $list[$k]['class_name'] = '';
            }

            //出勤学员列表
            $chuqin_sids = KaoqinXueyuan::getColumn(array('kid' => $v['id']), 'sid');
            $chuqin = [];
            foreach ($chuqin_sids as $chuqin_sid) {
                $chuqin[] = array(
                    'id' => $chuqin_sid,
                    'name' => isset($xueyuan[$chuqin_sid]) ? $xueyuan[$chuqin_sid] : Xueyuan::getValue(array('id' => $chuqin_sid), 'name')
                );
            }
            $list[$k]['chuqin'] = $chuqin;

            //未出勤学员列表
            $w = array(
                'cid' => $v['cid'],
                'sid' => array('not in', $chuqin_sids)
            );
            $weichuqin_sids = ClassesXueyuan::getColumn($w, 'sid');
            $weichuqin = [];
            $kaoqin_remark = KaoqinRemark::getColumn(array('kid'=>$v['id']) ,'sid,id');
            if ($weichuqin_sids) {
                foreach ($weichuqin_sids as $weichuqin_sid) {
                    $weichuqin[] = array(
                        'id' => $weichuqin_sid,
                        'name' => isset($xueyuan[$weichuqin_sid]) ? $xueyuan[$weichuqin_sid] : Xueyuan::getValue(array('id' => $weichuqin_sid), 'name'),
                        'kr_id'=>isset($kaoqin_remark[$weichuqin_sid])?$kaoqin_remark[$weichuqin_sid]:0
                    );
                }
                $list[$k]['weichuqin'] = $weichuqin;
            } else {
                $list[$k]['weichuqin'] = [];
            }


        }


        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total,
            'chuqin'=>$chuqin_num
        ]);
    }

    public function xueyuanDetail()
    {
        // 查询条件
        $where = [];

        if (input('id')) {
            $where['sid'] = array('eq', input('id'));
        }

        //获取列表
        $list = XueyuanJiaofei::getAll($where, 'lose_time desc');

        //考勤id
        $kids = KaoqinXueyuan::getColumn($where, 'kid', 'id desc');

        //考勤查询条件
        $k_w = array(
            'id' => array('in', $kids)
        );
        $data = [];

        foreach ($list as &$value) {
            $k_w['time'] = array(
                '>=' => date('Y-m-d', $value['pay_time']),
                '<=', date('Y-m-d', $value['lose_time'])
            );

            $d = KaoqinModel::getAll($k_w, 'id desc');

            if (!$d) {
                $d = [];
            }
            $data[] = $d;
        }

        //返回信息
        return returnJson(0, '', $data);
    }

    /**
     * 课表
     * @return void
     */
    public function kebiao()
    {
        //查询条件
        $where = array('status' => array('>', 0));
        //是否有查询条件教练id
        if (input('mid')) {
            $where['mid'] = input('mid');
        }
        //是否有查询条件月份
        if (input('month')) {
            $first = input('month') . '-1';//当月的第一天
            $month = input('month');//当月
            $t = date("t", strtotime($first));//最后一天
        } else {
            $first = date('Y-m-01');
            $month = date('Y-m');
            $t = date("t", strtotime($first));
        }
        //空白单元格
        $w = date('w', strtotime($first));
        $w = $w == 0 ? 6 : $w - 1;
        $data = [];
        for ($i = 0; $i < $w; $i++) {
            $data[] = array('list' => [], 'date' => '', 'day' => '');
        }
        //周次班级
        $w_list = [];
        for ($i = 1; $i <= 7; $i++) {
            $where['week'] = array('eq', $i);
            $w_list[$i] = Classes::getAll($where, 'time asc');
        }

        //停课
        $tk_where = array(
            'date' => array('like', $month . '%')
        );
        $tingke = ClassesTingke::getAll($tk_where);


        //日期课表
        for ($i = 1; $i <= $t; $i++) {
            $date = $month . '-' . $i;
            $date = date('Y-m-d', strtotime($date));

            $w = date('w', strtotime($date));
            $w = $w == 0 ? 7 : $w;

            $time = strtotime($date);

            $list = $w_list[$w];
            if ($list) {
                foreach ($list as $k => $v) {
                    //判断班级是否停课
                    if ($tingke) {
                        foreach ($tingke as $tk => $tv) {
                            if ($v['id'] == $tv['cid'] && $tv['date'] == $date) {
                                $list[$k]['status'] = 0;//停课
                            }
                        }
                    }
                    //判断一次班级
                    if (($v['status'] == 2) && (strtotime($date) != strtotime($v['date']))) {
                        unset($list[$k]);
                        continue;
                    }

                    //判断班级的开课时间
                    if (strtotime($v['create_time']) > $time) {
                        unset($list[$k]);
                        continue;
                    }

                    //判断是否已经考勤过了
                    $kq_where = array(
                        'time' => $date,
                        'cid' => $v['id']
                    );
                    $kaoqin = KaoqinModel::getOne($kq_where);

                    $list[$k]['kaoqin'] = $kaoqin;

                }
            }
            $data[] = array('list' => $list ? $list : [], 'date' => $i, 'day' => $month . '-' . $i);
        }
        return returnJson(0, '', $data);
    }

    /**
     * 班级考勤
     * @return void
     */
    public function classesKaoqin()
    {
        $sids = input('post.sids/a');
        $buke_sids = input('post.buke_sids/a');
        //考勤数据
        $save_kaoqin = array(
            'create_time' => time(),
            'update_time' => time(),
            'content' => input('content'),
            'manager_name' => input('manager_name'),
            'thumb' => input('thumb'),
            'time' => date('Y-m-d', strtotime(input('time'))),
            'cid' => input('cid'),
            'num' => count($sids) + count($buke_sids)
        );
        $id = KaoqinModel::setInsertGetId($save_kaoqin);

        //考勤学员
        $all_sids = array_merge($sids, $buke_sids);
        $save_kaoqin_xueyuan = [];
        foreach ($all_sids as $v) {
            $d = array(
                'create_time' => time(),
                'update_time' => time(),
                'sid' => $v,
                'kid' => $id
            );
            $save_kaoqin_xueyuan[] = $d;
        }
        KaoqinXueyuan::setSaveAll($save_kaoqin_xueyuan);
        return returnJson(0, '');

    }

    /**
     * 学员删除
     * @return void
     */
    public function delete()
    {
        // 查询条件
        $where = [];

        if(input('id')){
            $where['id']=array('eq',input('id'));
            KaoqinModel::setDel($where);
            KaoqinXueyuan::setDel(array('kid'=>input('id')));
        }

        //返回信息
        return returnJson(0, '');
    }

    /**
     * 未出勤备注原因
     * @return \think\response\Json
     */
    public function weichuqinRemark()
    {
        $kaoqin = KaoqinModel::getOne(array('id'=>input('kid')));
        $save = array(
            'update_time'=>time(),
            'create_time'=>time(),
            'cid'=>$kaoqin?$kaoqin['cid']:0,
            'time'=>$kaoqin?$kaoqin['time']:'',
            'sid'=>input('sid'),
            'remark'=>input('remark'),
            'thumb'=>input('thumb'),
            'kid'=>input('kid',0),
        );
        $kr_id = KaoqinRemark::setInsertGetId($save);
        //返回信息
        return returnJson(0, '',$kr_id);
    }

    /**
     * 审核
     * @return \think\response\Json
     */
    public function shenhe()
    {
        //判断是否进行数据更新
        if(input('kid')){

            //条件
            $where = array('id'=>input('kid'));

            //更新的信息
            $save = array(
                'update_time'=>time(),
                'guanliyuan_shenhe'=>input('guanliyuan_shenhe'),
                'coach_shenhe'=>input('coach_shenhe'),
                'status'=>input('status')
            );

            //更新
            KaoqinModel::update($save,$where);
        }

        //返回信息
        return returnJson(0, '');
    }


}
